package edu.hawaii.duedates.configuration;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import edu.hawaii.duedates.configuration.jaxb.DueDates;
import edu.hawaii.duedates.configuration.jaxb.Library;
import edu.hawaii.duedates.configuration.jaxb.User;

/**
 * Provides access to the contents of the "default" DueDates.
 * The default DueDates is stored in ~/.duedates/duedates.xml
 * @author Creighton Okada
 */
public class ConfigurationManager {
  /** The JAXB Stack instance read in from the XML file. */
  private DueDates dd;
  
  /**
   * Create a new ConfigurationManager that provides access to duedates in ~/.duedates/duedates.xml.
   */
  public ConfigurationManager() {
    this(new File(System.getProperty("user.home") + "/.duedates/duedates.xml"),
        new File(System.getProperty("user.home") + "/.duedates/duedates.xsd"));
  }

  /**
   * Creates a new ConfigurationManager that reads in the XML DueDates representation.
   * @param configFile An XML file containing a DueDates representation.
   * @param schemaFile An XML file containing a DueDates schema representation.
   */
  public ConfigurationManager(File configFile, File schemaFile) {
    try {
      JAXBContext jaxbContext = 
        JAXBContext.newInstance(edu.hawaii.duedates.configuration.jaxb.ObjectFactory.class);
      if (configFile.exists()) {
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        SchemaFactory schemaFactory = 
          SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = schemaFactory.newSchema(schemaFile);
        unmarshaller.setSchema(schema);
        this.dd = (DueDates) unmarshaller.unmarshal(configFile);
      }
    }
    
    catch (Exception e) {
      throw new RuntimeException("Error in DueDates ConfigurationManager", e);
    }
  }
  
  /**
   * Returns a DueDates object constructed from the default XML DueDates. 
   * If no default DueDates exists, an empty DueDates object is returned.
   * 
   * @param uid The user id.
   * @param pwd The password.
   * @return The DueDates instance constructed from the XML DueDates. 
   */
  public edu.hawaii.duedates.DueDates getDueDates(String uid, String pwd) {
    edu.hawaii.duedates.DueDates due = new edu.hawaii.duedates.DueDates();
    for (User user : dd.getUsers().getUser()) {
      if (user.getEmail().equals(uid) && user.getPassword().equals(pwd)) {   
        edu.hawaii.duedates.user.User newUser;
        // set user
        newUser = new edu.hawaii.duedates.user.User(user.getName(), user.getEmail(), 
            user.getPassword());
        due.setUser(newUser);        
        // set smtp
        due.setSmtpServer(dd.getSmtpServer());
        //set library
        List <edu.hawaii.duedates.library.Library> libraryList = 
          new ArrayList <edu.hawaii.duedates.library.Library>();
        for (Library library : user.getLibrary()) {
          edu.hawaii.duedates.library.Library lib = 
            new edu.hawaii.duedates.library.Library();
          lib.setLogin(library.getLogin());
          lib.setName(library.getName());
          lib.setPassword(library.getPassword());
          lib.setUniqueId(library.hashCode());
          lib.setLocation(library.getClazz());
          libraryList.add(lib);
        }
        // set libraries list equal to this user.getLibrary()
        due.setLibrary(libraryList);
        return due;
      }
    }
    return null;
  }
}